package com.cari.oa.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.SoftProjectEntity;
import com.cari.oa.domain.SoftRiskEntity;
import com.cari.oa.domain.SysDictItemEntity;
import com.cari.oa.service.SoftProjectService;
import com.cari.oa.service.SoftRiskService;
import com.cari.oa.service.SysDictItemService;
import com.cari.oa.util.Contants;
import com.cari.oa.util.Utils;

/**
 * 
* Title: SoftRiskController
* Description:风险control
* Company: cari 
* @author    hjx 
* @date       2013-1-24
 */
@Controller
@SessionAttributes("username")
public class SoftRiskController {

	@Resource
	private SysDictItemService sysDictItemService;
	@Resource
	private SoftRiskService softRiskService;
	@Resource
	private SoftProjectService softProjectService;
	public static final String MODULE_NAME = "SOFT_RISK_MANAGER";
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
	
    @RequestMapping("soft/riskManager/list.do")
    public String list(String riskType,String qs, String status,String projectId, Integer curPage, ModelMap modelMap) {
        if(curPage == null) curPage = 1;
        long totalRecords = softRiskService.getCount(riskType, status, projectId);
        long totalPage = totalRecords / Contants.PAGE_SIZE + (totalRecords % Contants.PAGE_SIZE != 0 ? 1 : 0);
        List<SoftRiskEntity> riskList = softRiskService.getPage(riskType, status, projectId, curPage, Contants.PAGE_SIZE);
    	SoftProjectEntity  softProjectEntity = softProjectService.getById(projectId);
        List<SysDictItemEntity> riskTypes = sysDictItemService.getItemsByGroupName("riskType");
    	List<SysDictItemEntity> riskStatus = sysDictItemService.getItemsByGroupName("riskStatus");
        for(SoftRiskEntity sre: riskList)
        {
        	if(sre.getDescription().length()>40)
        	{
        		sre.setDescription(sre.getDescription().substring(0,40)+"...");
        	}
        }
    	modelMap.addAttribute("riskTypes", riskTypes);
        modelMap.addAttribute("qs", qs);
       
        modelMap.addAttribute("riskList", riskList);
        modelMap.addAttribute("projectName", softProjectEntity.getName());
        modelMap.addAttribute("riskStatus", riskStatus);
        modelMap.addAttribute("curPage", curPage);
        modelMap.addAttribute("totalPage", totalPage);
        modelMap.addAttribute("totalRecords", totalRecords);
        modelMap.addAttribute("riskType", riskType);
        modelMap.addAttribute("status", status);
        modelMap.addAttribute("projectId", projectId);
        return "soft/risk/list";
    }
    
    @RequestMapping(value = "soft/riskManager/add.do", method = RequestMethod.GET)
    public String enterInsert(String projectId, String qs,ModelMap modelMap) {
    	List<SysDictItemEntity> itemList = sysDictItemService.getItemsByGroupName("riskType");
    	List<SysDictItemEntity> riskStatus = sysDictItemService.getItemsByGroupName("riskStatus");
    	SoftProjectEntity  softProjectEntity = softProjectService.getById(projectId);
        modelMap.addAttribute("itemList", itemList);
        modelMap.addAttribute("riskStatus", riskStatus);
        modelMap.addAttribute("projectId", projectId);
        modelMap.addAttribute("projectName",softProjectEntity.getName());
        return "soft/risk/insert";
    }
    
    @RequestMapping(value = "soft/riskManager/add.do", method = RequestMethod.POST)
    @ResponseBody
    public String postInsert(@ModelAttribute("username") String username, @Valid SoftRiskEntity softRiskEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());
        softRiskService.insert(softRiskEntity);
        SysOperLogController.addOperLog(username, MODULE_NAME, "新增风险" + softRiskEntity.getDescription());
        return Utils.normalJsonResp();
    }
    
    @RequestMapping(value = "soft/riskManager/view.do", method = RequestMethod.GET)
    public String enterView(String id,String projectName, ModelMap modelMap) {
    	List<SysDictItemEntity> riskTypes = sysDictItemService.getItemsByGroupName("riskType");
    	List<SysDictItemEntity> riskStatus = sysDictItemService.getItemsByGroupName("riskStatus");
    	modelMap.addAttribute("riskTypes", riskTypes);
        modelMap.addAttribute("riskStatus", riskStatus);
        SoftRiskEntity  risk = softRiskService.getById(id);
        modelMap.addAttribute("projectName", projectName);
        modelMap.addAttribute("risk", risk);
        return "soft/risk/view";
    }
    

    @RequestMapping(value = "soft/riskManager/edit.do", method = RequestMethod.GET)
    public String enterEdit(String id,String projectName, String qs, ModelMap modelMap) {
    	List<SysDictItemEntity> riskTypes = sysDictItemService.getItemsByGroupName("riskType");
    	List<SysDictItemEntity> riskStatus = sysDictItemService.getItemsByGroupName("riskStatus");
    	modelMap.addAttribute("riskTypes", riskTypes);
        modelMap.addAttribute("riskStatus", riskStatus);
        SoftRiskEntity  risk = softRiskService.getById(id);
        modelMap.addAttribute("projectName", projectName);
        modelMap.addAttribute("risk", risk);
        modelMap.addAttribute("qs", qs);
        return "soft/risk/edit";
    }
    
    @RequestMapping(value = "soft/riskManager/edit.do", method = RequestMethod.POST)
    @ResponseBody
    public String postEdit(@ModelAttribute("username") String username, @Valid SoftRiskEntity softRiskEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());
        softRiskService.update(softRiskEntity);
        SysOperLogController.addOperLog(username, MODULE_NAME, "修改风险失败！" + softRiskEntity.getDescription());
        return Utils.normalJsonResp();
    }
}
